From ed9f6cba7502c8bc7410803bf703c4f21fc96bf0 Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Mon, 7 Nov 2005 12:36:53 +0100 Subject: [PATCH] Move the randomMAC method from xm.create into server.netif. This way, it can be shared with other command line tools, and the xm network-attach command. Signed-off-by: Ewan Mellor --- tools/python/xen/xend/server/netif.py | 23 +++++++++++++++++++++ tools/python/xen/xm/create.py | 29 +++------------------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tools/python/xen/xend/server/netif.py b/tools/python/xen/xend/server/netif.py index d6f7671a6e..782ccf0215 100644 --- a/tools/python/xen/xend/server/netif.py +++ b/tools/python/xen/xend/server/netif.py @@ -21,6 +21,7 @@ """ import os +import random from xen.xend import sxp from xen.xend import XendRoot @@ -31,6 +32,25 @@ from xen.xend.server.DevController import DevController xroot = XendRoot.instance() +def randomMAC(): + """Generate a random MAC address. + + Uses OUI (Organizationally Unique Identifier) AA:00:00, an + unassigned one that used to belong to DEC. The OUI list is + available at 'standards.ieee.org'. + + The remaining 3 fields are random, with the first bit of the first + random field set 0. + + @return: MAC address string + """ + mac = [ 0xaa, 0x00, 0x00, + random.randint(0x00, 0x7f), + random.randint(0x00, 0xff), + random.randint(0x00, 0xff) ] + return ':'.join(map(lambda x: "%02x" % x, mac)) + + class NetifController(DevController): """Network interface controller. Handles all network devices for a domain. """ @@ -57,6 +77,9 @@ class NetifController(DevController): devid = self.allocateDeviceID() + if not mac: + mac = randomMAC() + back = { 'script' : script, 'mac' : mac, 'handle' : "%i" % devid } diff --git a/tools/python/xen/xm/create.py b/tools/python/xen/xm/create.py index c73d66c2f9..2a3d0929a6 100644 --- a/tools/python/xen/xm/create.py +++ b/tools/python/xen/xm/create.py @@ -19,7 +19,6 @@ """Domain creation. """ -import random import os import os.path import string @@ -497,24 +496,6 @@ def configure_tpmif(config_devs, vals): config_devs.append(['device', config_tpmif]) -def randomMAC(): - """Generate a random MAC address. - - Uses OUI (Organizationally Unique Identifier) AA:00:00, an - unassigned one that used to belong to DEC. The OUI list is - available at 'standards.ieee.org'. - - The remaining 3 fields are random, with the first bit of the first - random field set 0. - - @return: MAC address string - """ - mac = [ 0xaa, 0x00, 0x00, - random.randint(0x00, 0x7f), - random.randint(0x00, 0xff), - random.randint(0x00, 0xff) ] - return ':'.join(map(lambda x: "%02x" % x, mac)) - def configure_vifs(config_devs, vals): """Create the config for virtual network interfaces. """ @@ -525,8 +506,6 @@ def configure_vifs(config_devs, vals): if idx < len(vifs): d = vifs[idx] mac = d.get('mac') - if not mac: - mac = randomMAC() be_mac = d.get('be_mac') bridge = d.get('bridge') script = d.get('script') @@ -534,8 +513,7 @@ def configure_vifs(config_devs, vals): ip = d.get('ip') vifname = d.get('vifname') else: - - mac = randomMAC() + mac = None be_mac = None bridge = None script = None @@ -543,7 +521,8 @@ def configure_vifs(config_devs, vals): ip = None vifname = None config_vif = ['vif'] - config_vif.append(['mac', mac]) + if mac: + config_vif.append(['mac', mac]) if vifname: config_vif.append(['vifname', vifname]) if be_mac: @@ -925,8 +904,6 @@ def parseCommandLine(argv): def main(argv): - random.seed() - (opts, config) = parseCommandLine(argv) if not opts: -- 2.30.2